<template>
  <div>
    <el-breadcrumb separator-icon="ArrowRight" style="margin: 16px">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>申请管理</el-breadcrumb-item>
      <el-breadcrumb-item>请假申请</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card style="margin: 15px; min-height: calc(100vh - 111px)">
      <div>
        <!--    功能区-->
        <div style="margin: 10px 0">
          <!--    搜索区-->
          <div style="margin: 10px 0">
            <el-input
              v-model="search"
              clearable
              placeholder="请输入标题"
              prefix-icon="Search"
              style="width: 20%"
            />
            <el-button
              icon="Search"
              style="margin-left: 5px"
              type="primary"
              @click="stunameList"
            ></el-button>
            <el-button
              icon="refresh-left"
              style="margin-left: 10px"
              type="default"
              @click="reset"
            ></el-button>
            <!-- <div style="float: right">
              <el-tooltip content="添加" placement="top">
                <el-button
                  icon="plus"
                  style="width: 50px"
                  type="primary"
                  @click="add"
                ></el-button>
              </el-tooltip>
            </div> -->
          </div>
        </div>
        <!--    表格-->
        <el-table
          v-loading="loading"
          :data="tableData"
          border
          max-height="705"
          style="width: 100%"
        >
          <el-table-column label="#" type="index" />
          <el-table-column
            label="所在楼栋"
            prop="buildName"
            width="100px"
            align="center"
          />
          <el-table-column
            label="房间号"
            prop="roomNum"
            width="100px"
            align="center"
          />
          <el-table-column
            label="学生姓名"
            prop="studentName"
            width="100px"
            align="center"
          />
          <el-table-column
            label="请假原因"
            prop="reason"
            width="100px"
            align="center"
          />
          <!-- <el-table-column
            label="请假材料"
            prop="leaveMaterial"
            width="100px"
            align="center"
          /> -->
          <!-- <el-table-column label="请假材料">
            <template v-slot="scope">
              <span v-if="!scope.row.leaveMaterial">暂无</span>
              <el-button v-else size="small" type="warning">
                <a
                  :href="`D:\\a毕设\\springboot vue 宿舍管理系统\\springboot\\src\\main\\resources\\files\\${scope.row.leaveMaterial}`"
                  style="color: white"
                  download
                >
                  下载
                </a>
              </el-button>
            </template>
          </el-table-column> -->

          <el-table-column label="请假材料">
            <template v-slot="scope">
              <span v-if="!scope.row.leaveMaterial">暂无</span>
              <el-button v-else size="small" type="warning">
                <a
                  :href="require('@/assets/img/' + scope.row.leaveMaterial)"
                  style="color: white"
                  download
                >
                  下载
                </a>
              </el-button>
              <!-- <img
                :src="require('@/assets/img/' + scope.row.leaveMaterial)"
                style="width: 80px; height: 80px"
              /> -->
            </template>
          </el-table-column>

          <el-table-column
            label="离校去向"
            prop="leavePlace"
            width="200px"
            align="center"
          />
          <el-table-column
            label="离校日期"
            prop="leaveTime"
            sortable
            width="140px"
            align="center"
          />
          <el-table-column
            label="返校日期"
            prop="endTime"
            sortable
            width="220px"
            align="center"
          />
          <el-table-column
            label="辅导员"
            prop="teacherName"
            sortable
            width="100px"
            align="center"
          />
          <el-table-column
            :filter-method="filterTag"
            :filters="[
              { text: '待审批', value: '待审批' },
              { text: '已批准', value: '已批准' },
              { text: '已拒绝', value: '已拒绝' },
            ]"
            filter-placement="bottom-end"
            label="请假状态"
            prop="status"
            sortable
            width="150px"
            align="center"
          >
            <template #default="scope">
              <el-tag
                :type="
                  scope.row.status === '已批准'
                    ? 'success'
                    : scope.row.status === '已拒绝'
                    ? 'danger'
                    : 'info'
                "
                disable-transitions
                >{{ scope.row.status }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            label="备注"
            prop="noResaon"
            sortable
            width="220px"
            align="center"
          />
          <!--      操作栏-->
          <el-table-column
            v-if="this.user === 'teacher'"
            label="操作"
            width="130px"
          >
            <template #default="scope">
              <el-button
                v-if="scope.row.status !== '已批准'"
                icon="Edit"
                type="primary"
                @click="handleEdit(scope.row)"
              ></el-button>

              <el-popconfirm
                title="确认删除？"
                @confirm="handleDelete(scope.row.id)"
              >
                <template #reference>
                  <el-button icon="Delete" type="danger"></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>

          <!--      操作栏-->
          <!-- <el-table-column label="操作" width="130px">
            <template #default="scope">
              <el-button
                icon="Edit"
                type="primary"
                @click="handleEdit(scope.row)"
              ></el-button>
              <el-popconfirm
                title="确认删除？"
                @confirm="handleDelete(scope.row.id)"
              >
                <template #reference>
                  <el-button icon="Delete" type="danger"></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column> -->
        </el-table>
        <!--分页-->
        <div style="margin: 10px 0">
          <el-pagination
            v-model:currentPage="currentPage"
            :page-size="pageSize"
            :page-sizes="[10, 20]"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          >
          </el-pagination>
        </div>
        <!--      弹窗-->
        <div>
          <el-dialog
            v-model="dialogVisible"
            title="操作"
            width="30%"
            @close="cancel"
          >
            <el-form
              ref="form"
              :model="form"
              :rules="rules"
              label-width="120px"
            >
              <!-- <el-form-item label="请假材料" prop="leaveMaterial">
                <el-input
                  v-model="form.leaveMaterial"
                  clearable
                  style="width: 50%"
                ></el-input>
              </el-form-item> -->

              <el-form-item label="辅导员" prop="teacherName">
                <el-input
                  v-model="form.teacherName"
                  clearable
                  style="width: 50%"
                ></el-input>
              </el-form-item>

              <el-form-item label="请假状态" prop="status">
                <el-select
                  v-model="form.status"
                  placeholder="请输入申请状态"
                  style="width: 50%"
                >
                  <el-option label="待审批" value="待审批" />
                  <el-option label="通过审批" value="已批准" />
                  <el-option label="拒绝审批" value="已拒绝" />
                </el-select>
                <!-- <el-input
                  v-model="form.dormBuildId"
                  style="width: 80%"
                ></el-input> -->
              </el-form-item>
              <el-form-item label="备注" prop="noResaon">
                <el-input
                  v-model="form.noResaon"
                  clearable
                  style="width: 50%"
                ></el-input>
              </el-form-item>
            </el-form>
            <template #footer>
              <span class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
              </span>
            </template>
          </el-dialog>
        </div>
      </div>
    </el-card>
  </div>
</template>
<script>
import request from "@/utils/request";

const { ElMessage } = require("element-plus");

export default {
  name: "VisitorInfo",
  components: {},
  data() {
    return {
      loading: true,
      disabled: false,
      judge: false,
      dialogVisible: false,
      search: "",
      currentPage: 1,
      pageSize: 10,
      total: 0,
      tableData: [],
      detail: {},
      form: {},
      rules: {},
      user: {},
    };
  },
  created() {
    this.user = JSON.parse(sessionStorage.getItem("identity"));
    this.load();
    this.loading = true;
    setTimeout(() => {
      //设置延迟执行
      this.loading = false;
    }, 1000);
  },
  methods: {
    handle_success(res) {
      console.log(res.data);
      this.$message.success("图片上传成功");
      this.form.leaveMaterial = res.data;
    },
    async stunameList() {
      request
        .get("/leave/findByStuname", {
          params: {
            username: this.search, // 传递用户名参数
            pageNum: this.currentPage,
            pageSize: this.pageSize,
            search: "",
          },
        })
        .then((res) => {
          console.log(res);
          this.tableData = res.data.records;
          this.total = res.data.total;
          this.loading = false;
        });
    },
    async load() {
      const currentUser = JSON.parse(sessionStorage.getItem("user")); // 获取当前登录用户信息
      console.log(currentUser);
      const name = currentUser.name; // 假设用户信息中包含 username
      request
        .get("/leave/findByTeaname", {
          params: {
            username: name, // 传递用户名参数
            pageNum: this.currentPage,
            pageSize: this.pageSize,
            search: this.search,
          },
        })
        .then((res) => {
          console.log(res);
          this.tableData = res.data.records;
          this.total = res.data.total;
          this.loading = false;
        });
    },
    reset() {
      const currentUser = JSON.parse(sessionStorage.getItem("user")); // 获取当前登录用户信息
      console.log(currentUser);
      const name = currentUser.name; // 假设用户信息中包含 username
      this.search = "";
      request
        .get("/leave/findByTeaname", {
          params: {
            username: name, // 传递用户名参数
            pageNum: 1,
            pageSize: this.pageSize,
            search: this.search,
          },
        })
        .then((res) => {
          console.log(res);
          this.tableData = res.data.records;
          this.total = res.data.total;
          this.loading = false;
        });
    },
    filterTag(value, row) {
      return row.gender === value;
    },
    showDetail(row) {
      this.detailDialog = true;
      this.$nextTick(() => {
        this.detail = row;
      });
    },
    closeDetailDialog() {
      this.detailDialog = false;
    },
    add() {
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs.form.resetFields();
        this.disabled = false;
        this.form = {};
        this.judge = false;
      });
    },
    save() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          if (this.judge === false) {
            //新增
            await request.post("/leave/add", this.form).then((res) => {
              console.log(res);
              if (res.code === "0") {
                ElMessage({
                  message: "新增成功",
                  type: "success",
                });
                this.search = "";
                this.load();
                this.dialogVisible = false;
              } else {
                ElMessage({
                  message: res.msg,
                  type: "error",
                });
              }
            });
          } else {
            //修改
            await request.put("/leave/update", this.form).then((res) => {
              console.log(res);
              if (res.code === "0") {
                ElMessage({
                  message: "修改成功",
                  type: "success",
                });
                this.search = "";
                this.load();
                this.dialogVisible = false;
              } else {
                ElMessage({
                  message: res.msg,
                  type: "error",
                });
              }
            });
          }
        }
      });
    },
    cancel() {
      this.$refs.form.resetFields();
      this.dialogVisible = false;
    },
    handleEdit(row) {
      //修改
      this.judge = true;
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs.form.resetFields();
        // 生拷贝
        this.form = JSON.parse(JSON.stringify(row));
        this.disabled = true;
      });
    },
    async handleDelete(id) {
      console.log(id);
      request.delete("/leave/delete/" + id).then((res) => {
        if (res.code === "0") {
          ElMessage({
            message: "删除成功",
            type: "success",
          });
          this.search = "";
          this.load();
        } else {
          ElMessage({
            message: res.msg,
            type: "error",
          });
        }
      });
    },
    handleSizeChange(pageSize) {
      //改变每页个数
      this.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      //改变页码
      this.currentPage = pageNum;
      this.load();
    },
  },
};
</script>